home *** CD-ROM | disk | FTP | other *** search
/ One Click 14 / OneClick14.iso / Ferramentas / Convert XLS to Pdf / xls2pdf_setup.exe / {app} / lib / gs_setpd.ps < prev    next >
Encoding:
Text File  |  2002-11-01  |  26.6 KB  |  833 lines

  1. %    Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: gs_setpd.ps,v 1.17 2002/11/01 04:04:10 alexcher Exp $
  14. % The current implementation of setpagedevice has the following limitations:
  15. %    - It doesn't attempt to "interact with the user" for Policy = 2.
  16.  
  17. languagelevel 1 .setlanguagelevel
  18. level2dict begin
  19.  
  20. % ---------------- Redefinitions ---------------- %
  21.  
  22. % Redefine .beginpage and .endpage so that they call BeginPage and
  23. % EndPage respectively if appropriate.
  24.  
  25. % We have to guard against the BeginPage procedure not popping its operand.
  26. % This is really stupid, but the Genoa CET does it.
  27. /.beginpage {        % - .beginpage -
  28.   .currentshowpagecount {
  29.     .currentpagedevice pop
  30.     dup //null ne { /BeginPage .knownget } { pop //false } ifelse {
  31.         % Stack: ... pagecount proc
  32.        count 2 .execn
  33.         % Stack: ... ..???.. oldcount
  34.        count 1 add exch sub { pop } repeat
  35.     } {
  36.       pop
  37.     } ifelse
  38.   } if
  39. } bind odef
  40.  
  41. % Guard similarly against EndPage not popping its operand.
  42. /.endpage {        % <reason> .endpage <print_bool>
  43.   .currentshowpagecount {
  44.     1 index .currentpagedevice pop
  45.     dup //null ne { /EndPage .knownget } { pop //false } ifelse {
  46.         % Stack: ... reason pagecount reason proc
  47.       count 2 .execn
  48.         % Stack: ... ..???.. print oldcount
  49.       count 2 add exch sub { exch pop } repeat
  50.     } {
  51.       pop pop 2 ne
  52.     } ifelse
  53.   } {
  54.     2 ne
  55.   } ifelse
  56. } bind odef
  57.  
  58. % Define interpreter callouts for handling gstate-saving operators,
  59. % to make sure that they create a page device dictionary for use by
  60. % the corresponding gstate-restoring operator.
  61. % We'd really like to avoid the cost of doing this, but we don't see how.
  62. % The names %gsavepagedevice, %savepagedevice, %gstatepagedevice,
  63. % %copygstatepagedevice, and %currentgstatepagedevice are known to the
  64. % interpreter.
  65.  
  66. (%gsavepagedevice) cvn
  67.  { currentpagedevice pop gsave
  68.  } bind def
  69.  
  70. (%savepagedevice) cvn
  71.  { currentpagedevice pop save
  72.  } bind def
  73.  
  74. (%gstatepagedevice) cvn
  75.  { currentpagedevice pop gstate
  76.  } bind def
  77.  
  78. (%copygstatepagedevice) cvn
  79.  { currentpagedevice pop copy
  80.  } bind def
  81.  
  82. (%currentgstatepagedevice) cvn
  83.  { currentpagedevice pop currentgstate
  84.  } bind def
  85.  
  86. % Define interpreter callouts for handling gstate-restoring operators
  87. % when the current page device needs to be changed.
  88. % The names %grestorepagedevice, %grestoreallpagedevice,
  89. % %restorepagedevice, %restore1pagedevice, and %setgstatepagedevice
  90. % are known to the interpreter.
  91.  
  92. /.installpagedevice
  93.  {    % Since setpagedevice doesn't create new device objects,
  94.     % we must (carefully) reinstall the old parameters in
  95.     % the same device.
  96.    .currentpagedevice pop //null currentdevice //null .trysetparams
  97.    dup type /booleantype eq
  98.     { pop pop }
  99.     {        % This should never happen!
  100.       DEBUG { (Error in .trysetparams!) = pstack flush } if
  101.       cleartomark pop pop pop
  102.       /.installpagedevice cvx /rangecheck signalerror
  103.     }
  104.    ifelse pop pop
  105.     % A careful reading of the Red Book reveals that an erasepage
  106.     % should occur, but *not* an initgraphics.
  107.    erasepage .beginpage
  108.  } bind def
  109.  
  110. /.uninstallpagedevice
  111.  { 2 .endpage { .currentnumcopies //false .outputpage } if
  112.    nulldevice
  113.  } bind def
  114.  
  115. (%grestorepagedevice) cvn
  116.  { .uninstallpagedevice grestore .installpagedevice
  117.  } bind def
  118.  
  119. (%grestoreallpagedevice) cvn
  120.  { .uninstallpagedevice grestore .installpagedevice grestoreall
  121.  } bind def
  122.  
  123. (%restore1pagedevice) cvn
  124.  { .uninstallpagedevice grestore .installpagedevice restore
  125.  } bind def
  126.  
  127. (%restorepagedevice) cvn
  128.  { .uninstallpagedevice restore .installpagedevice
  129.  } bind def
  130.  
  131. (%setgstatepagedevice) cvn
  132.  { .uninstallpagedevice setgstate .installpagedevice
  133.  } bind def
  134.  
  135. % Redefine .currentnumcopies so it consults the NumCopies device parameter.
  136. /.numcopiesdict mark
  137.   /NumCopies dup
  138. .dicttomark readonly def
  139.  
  140. /.currentnumcopies
  141.  { currentdevice //.numcopiesdict .getdeviceparams
  142.    dup type /integertype eq
  143.     { exch pop exch pop }
  144.     { cleartomark #copies }
  145.    ifelse
  146.  } bind odef
  147.  
  148. % Redefine .currentpagedevice and .setpagedevice so they convert between
  149. % null and a fixed empty directionary.
  150. /.nullpagedevice 0 dict readonly def
  151. /.currentpagedevice {
  152.   //.currentpagedevice exch dup //null eq { pop //.nullpagedevice } if exch
  153. } bind odef
  154. /.setpagedevice {
  155.   dup //.nullpagedevice eq { pop //null } if //.setpagedevice
  156. } bind odef
  157.  
  158. % ---------------- Auxiliary definitions ---------------- %
  159.  
  160. % Define the required attributes of all page devices, and their default values.
  161. % We don't include attributes such as .MediaSize, which all devices
  162. % are guaranteed to supply on their own.
  163. /.defaultpolicies mark
  164.   /PolicyNotFound 1
  165.   /PageSize 0
  166.   /PolicyReport {
  167.     dup /.LockSafetyParams known {
  168.     % Only possible error is invalidaccess
  169.       /setpagedevice .systemvar /invalidaccess signalerror
  170.     }
  171.     if
  172.     pop
  173.   } bind
  174. .dicttomark readonly def
  175. % Note that the values of .requiredattrs are executed, not just fetched.
  176. /.requiredattrs mark
  177.   /PageDeviceName //null
  178.   /PageOffset [0 0] readonly
  179. % We define InputAttributes and OutputAttributes with a single
  180. % dummy media type that handles pages of any size.
  181. % Devices that care will override this.
  182.   /InputAttributes {
  183.     mark 0
  184.     % PageSize with either dimension 0 will be detected in
  185.     % match_page_size, so we can allow it here
  186.     mark /PageSize [0 dup 16#7ffff dup] .dicttomark
  187.     .dicttomark
  188.   }
  189.   (%MediaSource) 0
  190.   /OutputAttributes {
  191.     mark 0 mark .dicttomark readonly .dicttomark
  192.   }
  193.   (%MediaDestination) 0
  194.   /Install {{.callinstall}} bind
  195.   /BeginPage {{.callbeginpage}} bind
  196.   /EndPage {{.callendpage}} bind
  197.   /Policies .defaultpolicies
  198.   /ImagingBBox //null        % default value
  199.   /UseCIEColor /.getuseciecolor load
  200. .dicttomark readonly def
  201.  
  202. % Define currentpagedevice so it creates the dictionary on demand if needed,
  203. % adding all the required entries defined just above.
  204. % We have to deal specially with entries that the driver may change
  205. % on its own.
  206. /.dynamicppkeys mark
  207.   /.MediaSize dup        % because it changes when PageSize is set
  208.   /PageCount dup
  209.   /Colors dup
  210.   /BitsPerPixel dup
  211.   /ColorValues dup
  212. .dicttomark readonly def
  213. /.makecurrentpagedevice {    % - .makecurrentpagedevice <dict>
  214.   currentdevice //null .getdeviceparams
  215.     % Make the dictionary large enough to add defaulted entries.
  216.   counttomark 2 idiv .requiredattrs length add dict
  217.   counttomark 2 idiv { dup 4 2 roll put } repeat exch pop
  218.     % Add any missing required attributes.
  219.     % Make a writable and (if possible) local copy of any default
  220.     % dictionaries, to work around a bug in the output of WordPerfect,
  221.     % which assumes that these dictionaries are writable and local.
  222.   .currentglobal exch dup gcheck .setglobal
  223.   .requiredattrs {
  224.     2 index 2 index known {
  225.       1 index /Policies eq {
  226.     % Merge policies from the device driver with defaults
  227.     2 index             % <<>> /key value <<>>
  228.     3 2 roll get        % <<>> value <<policies>>
  229.     exch {                
  230.       2 index 2 index known { 
  231.         pop pop
  232.       } { 
  233.         2 index 3 1 roll put
  234.       } ifelse
  235.     } forall
  236.     pop
  237.       } {
  238.     pop pop
  239.       } ifelse
  240.     } {
  241.       exec 2 index 3 1 roll put
  242.     } ifelse
  243.   } forall exch .setglobal
  244.   dup .setpagedevice
  245. } bind def
  246. /currentpagedevice {
  247.   .currentpagedevice {
  248.     dup length 0 eq {
  249.       pop .makecurrentpagedevice
  250.     } {
  251.         % If any of the dynamic keys have changed,
  252.         % we must update the page device dictionary.
  253.       currentdevice //.dynamicppkeys .getdeviceparams .dicttomark {
  254.         % Stack: current key value
  255.         2 index 2 index .knownget { 1 index ne } { //true } ifelse
  256.          { 2 index wcheck not
  257.         {    % This is the first entry being updated.
  258.             % Copy the dictionary to make it writable.
  259.           3 -1 roll
  260.           currentglobal 1 index dup gcheck currentglobal and setglobal
  261.           length dict
  262.           exch setglobal
  263.           .copydict
  264.           3 1 roll
  265.         }
  266.            if
  267.            2 index 3 1 roll put
  268.          }
  269.          { pop pop
  270.          }
  271.         ifelse
  272.       } forall
  273.         % If the dictionary was global and is now local, copy
  274.         % any global subsidiary dictionaries to local VM.  This
  275.         % too is to work around the Word Perfect bug (see above).
  276.       dup gcheck not {
  277.     dup {
  278.       dup type /dicttype eq { dup gcheck } { //false } ifelse {
  279.         % Copy-on-write, see above.
  280.         2 index wcheck not {
  281.           3 -1 roll dup length dict .copydict
  282.           3 1 roll
  283.         } if
  284.         .copytree 2 index 3 1 roll put
  285.       } {
  286.         pop pop
  287.       } ifelse
  288.     } forall
  289.       } if
  290.         % We would like to do a .setpagedevice so we don't keep
  291.         % re-creating the dictionary.  Unfortunately, the effect
  292.         % of this is that if any dynamic key changes (PageCount
  293.         % in particular), we will do the equivalent of a
  294.         % setpagedevice at the next restore or grestore.
  295.         % Therefore, we make the dictionary read-only, but
  296.         % we don't store it away.  I.e., NOT:
  297.         % dup wcheck { .setpagedevice .currentpagedevice pop } if
  298.       readonly
  299.     } ifelse
  300.   } if
  301. } bind odef
  302.  
  303. % Copy a dictionary recursively.
  304. /.copytree {    % <dict> .copytree <dict'>
  305.   dup length dict exch {
  306.     dup type /dicttype eq { .copytree } if 2 index 3 1 roll put
  307.   } forall
  308. } bind def
  309.  
  310. % The implementation of setpagedevice is quite complex.  Currently,
  311. % everything but the media matching algorithm is implemented here.
  312.  
  313. % By default, we only present the requested changes to the device,
  314. % but there are some parameters that require special merging action.
  315. % Define those parameters here, with the procedures that do the merging.
  316. % The procedures are called as follows:
  317. %    <merged> <key> <new_value> -proc- <merged> <key> <new_value'>
  318. /.mergespecial mark
  319.   /InputAttributes
  320.    { dup //null eq
  321.       { pop //null
  322.       }
  323.       { 3 copy pop .knownget
  324.      { dup //null eq
  325.         { pop dup length dict }
  326.         { dup length 2 index length add dict .copydict }
  327.        ifelse
  328.      }
  329.      { dup length dict
  330.      }
  331.         ifelse .copydict readonly
  332.       }
  333.      ifelse
  334.    } bind
  335.   /OutputAttributes 1 index
  336.   /Policies
  337.     { 3 copy pop .knownget
  338.        { dup length 2 index length add dict .copydict }
  339.        { dup length dict }
  340.       ifelse copy readonly
  341.     } bind
  342. .dicttomark readonly def
  343.  
  344. % Define the keys used in input attribute matching.
  345. /.inputattrkeys [
  346.   /PageSize /MediaColor /MediaWeight /MediaType /InsertSheet
  347.     % The following are documented in Adobe's supplement for v2017.
  348.   /LeadingEdge /MediaClass
  349. ] readonly def
  350. % Define other keys used in media selection.
  351. /.inputselectionkeys [
  352.   /MediaPosition /Orientation
  353. ] readonly def
  354.  
  355. % Define the keys used in output attribute matching.
  356. /.outputattrkeys [
  357.   /OutputType
  358. ] readonly def
  359.  
  360. % Define all the parameters that should always be copied to the merged
  361. % dictionary.
  362. /.copiedkeys [
  363.   /OutputDevice
  364.   .mergespecial { pop } forall
  365.   .inputattrkeys aload pop
  366.   .inputselectionkeys aload pop
  367.   .outputattrkeys aload pop
  368. ] readonly def
  369.  
  370. % Define the parameters that should not be presented to the device.
  371. % The procedures are called as follows:
  372. %    <merged> <key> <value> -proc-
  373. % The procedure leaves all its operands on the stack and returns
  374. % true iff the key/value pair should be presented to .putdeviceparams.
  375. /.presentspecial mark
  376.   .dynamicppkeys { pop //false } forall
  377.             % We must ignore an explicit request for .MediaSize,
  378.             % because media matching always handles this.
  379.   /.MediaSize //false
  380.   /Name //false
  381.   /OutputDevice //false
  382.   /PageDeviceName //false
  383.   /PageOffset //false
  384.   /PageSize //false        % obsolete alias for .MediaSize
  385.   /InputAttributes //false
  386.   .inputattrkeys
  387.     { dup /PageSize eq
  388.        { pop }
  389.        { { 2 index /InputAttributes .knownget { //null eq } { //true } ifelse } }
  390.       ifelse
  391.     }
  392.   forall
  393.   .inputselectionkeys { //false } forall
  394.   /OutputAttributes //false
  395.   .outputattrkeys
  396.     { { 2 index /OutputAttributes .knownget { //null eq } { //true } ifelse } }
  397.   forall
  398.   /Install //false
  399.   /BeginPage //false
  400.   /EndPage //false
  401.   /Policies //false
  402.     % Our extensions:
  403.   /HWColorMap
  404.     {            % HACK: don't transmit the color map, because
  405.             % window systems can change the color map on their own
  406.             % incrementally.  Someday we'll have a better
  407.             % solution for this....
  408.       //false
  409.     }
  410.   /ViewerPreProcess //false
  411.   /ImagingBBox //false    % This prevents the ImagingBBox value in the setpagedevice
  412.             % from affecting the device's ImagingBBox parameter, but
  413.             % does retain a 'shadow' copy at the PostScript level.
  414.             % This is done for Adobe compatibility since Adobe does
  415.             % render marks outside the ImagingBBox (and QuarkXpress
  416.             % relies on it).
  417. .dicttomark readonly def
  418.  
  419. % Define access to device defaults.
  420. /.defaultdeviceparams
  421.  { finddevice //null .getdeviceparams
  422.  } bind def
  423.  
  424. % Select media (input or output).  The hard work is done in an operator:
  425. %    <pagedict> <attrdict> <policydict> <keys> .matchmedia <key> true
  426. %    <pagedict> <attrdict> <policydict> <keys> .matchmedia false
  427. %    <pagedict> null <policydict> <keys> .matchmedia null true
  428. /.selectmedia        % <orig> <request> <merged> <failed>     <-- retained
  429.             %   <attrdict> <policydict> <attrkeys> <mediakey>
  430.             %   .selectmedia
  431.  { 5 index 5 -2 roll 4 index .matchmedia
  432.         % Stack: orig request merged failed attrkeys mediakey
  433.         %   (key true | false)
  434.     { 4 index 3 1 roll put pop
  435.     }
  436.     {    % Adobe's implementations have a "big hairy heuristic"
  437.     % to choose the set of keys to report as having failed the match.
  438.     % For the moment, we report any keys that are in the request
  439.     % and don't have the same value as in the original dictionary.
  440.       5 index 1 index .knownget
  441.        { 4 index 3 1 roll put }
  442.        { 3 index exch .undef }
  443.       ifelse
  444.        {    % Stack: <orig> <request> <merged> <failed> <attrkey>
  445.      3 index 1 index .knownget
  446.       { 5 index 2 index .knownget { ne } { pop //true } ifelse }
  447.       { //true }
  448.      ifelse        % Stack: ... <failed> <attrkey> <report>
  449.       { 2 copy /rangecheck put }
  450.      if pop
  451.        }
  452.       forall
  453.     }
  454.    ifelse
  455.  } bind def
  456.  
  457. % Apply Policies to any unprocessed failed requests.
  458. % As we process each request entry, we replace the error name
  459. % in the <failed> dictionary with the policy value,
  460. % and we replace the key in the <merged> dictionary with its prior value
  461. % (or remove it if it had no prior value).
  462. /.policyprocs mark
  463. % These procedures are called with the following on the stack:
  464. %   <orig> <merged> <failed> <Policies> <key> <policy>
  465. % They are expected to consume the top 2 operands.
  466. % NOTE: we currently treat all values other than 0, 1, or 7 (for PageSize)
  467. % the same as 0, i.e., we signal an error.
  468.   0 {        % Set errorinfo and signal a configurationerror.
  469.     pop dup 4 index exch get 2 array astore
  470.     $error /errorinfo 3 -1 roll put
  471.     cleartomark
  472.     /setpagedevice load /configurationerror signalerror
  473.   } bind
  474.   1 {        % Roll back the failed request to its previous status.
  475. DEBUG { (Rolling back.) = pstack flush } if
  476.     3 index 2 index 3 -1 roll put
  477.     4 index 1 index .knownget
  478.      { 4 index 3 1 roll put }
  479.      { 3 index exch .undef }
  480.     ifelse
  481.   } bind
  482.   7 {        % For PageSize only, just impose the request.
  483.     1 index /PageSize eq
  484.      { pop pop 1 index /PageSize 7 put }
  485.      { .policyprocs 0 get exec }
  486.     ifelse
  487.   } bind
  488. .dicttomark readonly def
  489. /.applypolicies        % <orig> <merged> <failed> .applypolicies
  490.             %   <orig> <merged'> <failed'>
  491.  { 1 index /Policies get 1 index
  492.     { type /integertype eq
  493.        { pop        % already processed
  494.        }
  495.        { 2 copy .knownget not { 1 index /PolicyNotFound get } if
  496.             % Stack: <orig> <merged> <failed> <Policies> <key>
  497.             %   <policy>
  498.      .policyprocs 1 index .knownget not { .policyprocs 0 get } if exec
  499.        }
  500.       ifelse
  501.     }
  502.    forall pop
  503.  } bind def
  504.  
  505. % Prepare to present parameters to the device, by spreading them onto the
  506. % operand stack and removing any that shouldn't be presented.
  507. /.prepareparams        % <params> .prepareparams -mark- <key1> <value1> ...
  508.  { mark exch dup
  509.     {            % Stack: -mark- key1 value1 ... merged key value
  510.       .presentspecial 2 index .knownget
  511.        { exec { 3 -1 roll } { pop pop } ifelse }
  512.        { 3 -1 roll }
  513.       ifelse
  514.     }
  515.    forall pop
  516.  } bind def
  517.  
  518. % Put device parameters without resetting currentpagedevice.
  519. % (.putdeviceparams clears the current page device.)
  520. /.putdeviceparamsonly    % <device> <Policies|null> <require_all> -mark-
  521.             %   <key1> <value1> ... .putdeviceparamsonly
  522.             % On success: <device> <eraseflag>
  523.             % On failure: <device> <Policies|null> <req_all> -mark-
  524.             %   <key1> <error1> ...
  525.  { .currentpagedevice
  526.     { counttomark 4 add 1 roll .putdeviceparams
  527.       dup type /booleantype eq { 3 } { counttomark 5 add } ifelse -1 roll
  528.       .setpagedevice
  529.     }
  530.     { pop .putdeviceparams
  531.     }
  532.    ifelse
  533.  } bind def
  534.  
  535. % Try setting the device parameters from the merged request.
  536. /.trysetparams        % <merged> <(ignored)> <device> <Policies>
  537.             %   .trysetparams
  538.  { //true 4 index .prepareparams
  539.             % Add the computed .MediaSize.
  540.             % Stack: merged (ignored) device Policies -true-
  541.             %   -mark- key1 value1 ...
  542.    counttomark 5 add index .computemediasize
  543.    exch pop exch pop /.MediaSize exch
  544. DEBUG { (Putting.) = pstack flush } if
  545.    .putdeviceparamsonly
  546. DEBUG { (Result of putting.) = pstack flush } if
  547.  } bind def
  548.  
  549. % Compute the media size and initial matrix from a merged request (after
  550. % media selection).
  551. /.computemediasize    % <request> .computemediasize
  552.             %   <request> <matrix> <[width height]>
  553.  { dup /PageSize get                    % requested page size
  554.    1 index /InputAttributes get
  555.      2 index (%MediaSource) get get /PageSize get    % media size
  556.                             % (may be a range)
  557.    2 index /Policies get
  558.      dup /PageSize .knownget
  559.       { exch pop } { /PolicyNotFound get } ifelse    % PageSize policy,
  560.                             % affects scaling
  561.    3 index /Orientation .knownget not { //null } if
  562.    4 index /RollFedMedia .knownget not { //false } if
  563.    matrix .matchpagesize not {
  564.         % This is a "can't happen" condition!
  565.      /setpagedevice load /rangecheck signalerror
  566.    } if
  567.    2 array astore
  568.  } bind def
  569.  
  570. % ---------------- setpagedevice itself ---------------- %
  571.  
  572. /setpagedevice
  573.  {        % We mustn't pop the argument until the very end,
  574.         % so that the pseudo-operator machinery can restore the stack
  575.         % if an error occurs.
  576.    mark 1 index currentpagedevice
  577.  
  578.         % Check whether we are changing OutputDevice;
  579.         % also handle the case where the current device
  580.         % is not a page device.
  581.         % Stack: mark <request> <current>
  582. DEBUG { (Checking.) = pstack flush } if
  583.  
  584.    dup /OutputDevice .knownget
  585.     {        % Current device is a page device.
  586.       2 index /OutputDevice .knownget
  587.        {    % A specific OutputDevice was requested.
  588.      2 copy eq
  589.       { pop pop //null }
  590.       { exch pop }
  591.      ifelse
  592.        }
  593.        { pop //null
  594.        }
  595.       ifelse
  596.     }
  597.     {        % Current device is not a page device.
  598.         % Use the default device.
  599.       1 index /OutputDevice .knownget not { .defaultdevicename } if
  600.     }
  601.    ifelse
  602.    dup //null eq
  603.     { pop
  604.     }
  605.     { exch pop .defaultdeviceparams
  606.         % In case of duplicate keys, .dicttomark takes the entry
  607.         % lower on the stack, so we can just append the defaults here.
  608.       .requiredattrs { exec } forall .dicttomark
  609.     }
  610.    ifelse
  611.  
  612.         % Check whether a viewer wants to intervene.
  613.         % We must check both the request (which takes precedence)
  614.         % and the current dictionary.
  615.         % Stack: mark <request> <orig>
  616.    exch dup /ViewerPreProcess .knownget
  617.     { exec }
  618.     { 1 index /ViewerPreProcess .knownget { exec } if }
  619.    ifelse exch
  620.  
  621.         % Construct a merged request from the actual request plus
  622.         % any keys that should always be propagated.
  623.         % Stack: mark <request> <orig>
  624. DEBUG { (Merging.) = pstack flush } if
  625.  
  626.    exch 1 index length 1 index length add dict
  627.    .copiedkeys
  628.     {        % Stack: <orig> <request> <merged> <key>
  629.       3 index 1 index .knownget { 3 copy put pop } if pop
  630.     }
  631.    forall
  632.         % Stack: <orig> <request> <merged>
  633.    dup 2 index
  634.     {        % stack: <orig> <request> <merged> <merged> <rkey> <rvalue>
  635.       .mergespecial 2 index .knownget { exec } if
  636.       put dup
  637.     }
  638.    forall pop
  639.         % Hack: if FIXEDRESOLUTION is true, discard any attempt to
  640.         % change HWResolution.
  641.    FIXEDRESOLUTION { dup /HWResolution .undef } if
  642.         % Hack: if FIXEDMEDIA is true, discard any attempt to change
  643.         % PageSize or HWSize.
  644.    FIXEDMEDIA
  645.     { dup /PageSize 4 index /PageSize get put
  646.       dup /HWSize 4 index /HWSize get put
  647.     } if
  648.         % Hack: to work around some files that take a PageSize
  649.         % from InputAttributes and impose it, discard any attempt
  650.         % to set PageSize to a 4-element value.
  651.         % Stack: mark <orig> <request> <merged>
  652.     dup /PageSize .knownget {
  653.       length 2 ne {
  654.     dup /PageSize 4 index /PageSize get put
  655.       } if
  656.     } if
  657.  
  658.         % Select input and output media.
  659.         % Stack: mark <orig> <request> <merged>
  660. DEBUG { (Selecting.) = pstack flush } if
  661.  
  662.    0 dict    % <failed>
  663.    1 index /InputAttributes .knownget
  664.     { 2 index /Policies get
  665.       .inputattrkeys (%MediaSource) cvn .selectmedia
  666.     } if
  667.    1 index /OutputAttributes .knownget
  668.     { 2 index /Policies get
  669.       .outputattrkeys (%MediaDestination) cvn .selectmedia
  670.      } if
  671.    3 -1 roll 4 1 roll        % temporarily swap orig & request
  672.    .applypolicies
  673.    3 -1 roll 4 1 roll        % swap back
  674.  
  675.         % Construct the new device, and attempt to set its attributes.
  676.         % Stack: mark <orig> <request> <merged> <failed>
  677. DEBUG { (Constructing.) = pstack flush } if
  678.  
  679.    currentdevice .devicename 2 index /OutputDevice get eq
  680.     { currentdevice }
  681.     { 1 index /OutputDevice get finddevice }
  682.    ifelse
  683.         %**************** We should copy the device here,
  684.         %**************** but since we can't close the old device,
  685.         %**************** we don't.  This is WRONG.
  686.     %****************copydevice
  687.    2 index /Policies get
  688.    .trysetparams
  689.    dup type /booleantype ne
  690.     {        % The request failed.
  691.         % Stack: ... <orig> <request> <merged> <failed> <device>
  692.         %   <Policies> true mark <name> <errorname> ...
  693. DEBUG { (Recovering.) = pstack flush } if
  694.       counttomark 4 add index
  695.       counttomark 2 idiv { dup 4 -2 roll put } repeat
  696.       pop pop pop
  697.         % Stack: mark ... <orig> <request> <merged> <failed> <device>
  698.         %   <Policies>
  699.       6 2 roll 3 -1 roll 4 1 roll
  700.       .applypolicies
  701.       3 -1 roll 4 1 roll 6 -2 roll
  702.       .trysetparams        % shouldn't fail!
  703.       dup type /booleantype ne
  704.        { 2 { counttomark 1 add 1 roll cleartomark } repeat
  705.          /setpagedevice load exch signalerror
  706.        }
  707.       if
  708.     }
  709.    if
  710.  
  711.         % The attempt succeeded.  Install the new device.
  712.         % Stack: mark ... <merged> <failed> <device> <eraseflag>
  713. DEBUG { (Installing.) = pstack flush } if
  714.  
  715.    pop 2 .endpage
  716.     { 1 //true .outputpage
  717.       (>>setpagedevice, press <return> to continue<<\n) .confirm
  718.     }
  719.    if
  720.         % .setdevice clears the current page device!
  721.    .currentpagedevice pop exch
  722.    .setdevice pop
  723.    .setpagedevice
  724.  
  725.         % Implement UseCIEColor directly if this is a LL3 system.
  726.         % The color substitution feature is now implemented in
  727.         % the interpreter, and this is used as an optimization.
  728.         %
  729.         % NB: This shoud be the only use of the .setuseciecolor
  730.         %     operator anywhere.
  731.         %
  732.         % If UseCIEColor is transitioned to false, set some
  733.         % color space other than /DeviceGray, to insure that
  734.         % initgraphics will actually perform a setcolorspace
  735.         % operation (there is an optimization in setcolorspace
  736.         % that does nothing if the operand and current color
  737.         % spaces are the same, and UseCIEColor is false).
  738.  
  739.     /.setuseciecolor where
  740.       {
  741.         pop 1 index /UseCIEColor .knownget
  742.           {
  743.             dup .setuseciecolor not
  744.               { /DeviceRGB setcolorspace }
  745.             if
  746.           }
  747.         if
  748.       }
  749.     if
  750.  
  751.         % Merge the request into the current page device,
  752.         % unless we're changing the OutputDevice.
  753.         % Stack: mark ... <merged> <failed>
  754.    exch currentpagedevice dup length 2 index length add dict
  755.         % Stack: mark ... <failed> <merged> <current> <newdict>
  756.    2 index /OutputDevice .knownget {
  757.      2 index /OutputDevice .knownget not { //null } if eq
  758.    } {
  759.      //true
  760.    } ifelse {
  761.         % Same OutputDevice, merge the dictionaries.
  762.      .copydict
  763.    } {
  764.         % Different OutputDevice, discard the old dictionary.
  765.      exch pop
  766.    } ifelse .copydict
  767.         % Initialize the default matrix, taking media matching
  768.         % into account.
  769.    .computemediasize pop initmatrix concat
  770.    dup /PageOffset .knownget
  771.     {        % Translate by the given number of 1/72" units in device X/Y.
  772.       dup 0 get exch 1 get
  773.       2 index /HWResolution get dup 1 get exch 0 get
  774.       4 -1 roll mul 72 div   3 1 roll mul 72 div
  775.       idtransform translate
  776.     }
  777.    if
  778.         % We must install the new page device dictionary
  779.         % before calling the Install procedure.
  780.   dup .setpagedevice
  781.   .setdefaultscreen    % Set the default screen before calling Install.
  782.   dup /Install .knownget {
  783.     { .execinstall } stopped { .postinstall stop } { .postinstall } ifelse
  784.   } {
  785.     .postinstall
  786.   } ifelse
  787. } odef
  788.  
  789. % We break out the code after calling the Install procedure into a
  790. % separate procedure, since it is executed even if Install causes an error.
  791. % By making .execinstall a separate operator procedure, we get the stacks
  792. % restored if it fails.
  793.  
  794. /.execinstall {        % <proc> .execinstall -
  795.     % Because the interpreter optimizes tail calls, we can't just let
  796.     % the body of this procedure be 'exec', because that would lose
  797.     % the stack protection that is the whole reason for having the
  798.     % procedure in the first place.  We hack this by adding a couple
  799.     % of extra tokens to ensure that the operator procedure is still
  800.     % on the stack during the exec.
  801.   exec
  802.   0 pop    % See above.
  803. } odef
  804. /.postinstall {        % mark ... <failed> <merged> .postinstall -
  805.    matrix currentmatrix .setdefaultmatrix
  806.         % Erase and initialize the page.
  807.    initgraphics
  808.    currentoverprint //false setoverprint 1 setcolor
  809.    .fillpage
  810.    0 setcolor setoverprint
  811.    .beginpage
  812.  
  813.         % Clean up, calling PolicyReport if needed.
  814.         % Stack: mark ... <failed> <merged>
  815. DEBUG { (Finishing.) = pstack flush } if
  816.  
  817.    exch dup length 0 ne
  818.     { 1 index /Policies get /PolicyReport get
  819.       counttomark 1 add 2 roll cleartomark
  820.       exec
  821.     }
  822.     { cleartomark
  823.     }
  824.    ifelse pop
  825.  
  826. } odef
  827.  
  828. end                % level2dict
  829. .setlanguagelevel
  830.